package com.tssm.action;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.tssm.cache.memcached.TsSessionUtil;
import com.tssm.core.util.FormatUtil;
import com.tssm.core.util.MD5;
import com.tssm.core.util.RequestUtils;
import com.tssm.pojo.SarcSupplier;
import com.tssm.service.SarcSupplierService;

@Controller("loginAction")
@Scope("prototype")
public class LoginAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private String username;

	private String password;

	private String rand;

	private String message;
	@Resource
	private SarcSupplierService sarcsupplierService;

	private MD5 md5 = new MD5();

	public String login() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpSession session = request.getSession();
		String random = (String) session.getAttribute("random"); // 从session里获取验证码
		if (!rand.toLowerCase().equals(random)) {
			message="验证码输入错误！";
			return "loginerr";
		} else {
			if (username != null && !("").equals(username)) {
				username = username.trim();
			}
			Cookie tscookie = RequestUtils.getCookie(request, "wwwtscom");
			if (tscookie == null) {
				SarcSupplier supplier = null;
				if (password != null && !("").equals(password)) {
					String pswd = md5.calcMD5(password);
					supplier = sarcsupplierService.findByUserAccount(username,
							pswd);
				}
				if (supplier == null) {
					message="用户名或密码错误";
					return "loginerr";
				} else {
					TsSessionUtil.refurbishUser(response, supplier);
					Date date = new Date();
					String sdate = FormatUtil.getFormatTime(
							FormatUtil.FORMAT_THREE, date);// 拿到系统当前时间
					supplier.setDef1(sdate);
					sarcsupplierService.update(supplier);
					TsSessionUtil.refurbishUser(response, supplier);
					return "loginsuc";
				}
			} else {
				TsSessionUtil.invalidate(request, response);
				return "loginerr";
			}
		}
	}

	public String islogin() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		SarcSupplier nfuser = TsSessionUtil.getTsUser(request);
		if (nfuser != null) {
			message = "登录成功！";
		}
		return "islogin";
	}

	/**
	 * 
	 * 
	 * 
	 * @方法说明: 用户退出
	 * @return
	 * @throws Exception
	 * @作者:徐龙
	 * @日期:2011-12-18
	 */
	public String tsLogout() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		TsSessionUtil.invalidate(request, response);
		return "tsindex";
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getRand() {
		return rand;
	}

	public void setRand(String rand) {
		this.rand = rand;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
